# -*- coding: utf-8 -*-
from __future__ import absolute_import

from PyQt4.QtGui import QWidget
from ninja_ide.core import settings
from PyQt4.QtGui import QTabWidget
from PyQt4.QtGui import QVBoxLayout
from PyQt4.QtGui import QSplitter
from PyQt4.QtCore import Qt
from PyQt4.QtCore import SIGNAL


__centralWidgetInstance = None


def CentralWidget(*args, **kw):
    global __centralWidgetInstance
    if __centralWidgetInstance is None:
        __centralWidgetInstance = __CentralWidget(*args, **kw)
    return __centralWidgetInstance


class __CentralWidget(QWidget):

###############################################################################
# CentralWidget SIGNALS
###############################################################################

    """
    splitterCentralRotated()
    """

###############################################################################

    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        vbox = QVBoxLayout(self)
        #Create Splitters to divide the UI in: MainPanel, Explorer, Misc
        self._splitterArea = QSplitter(Qt.Horizontal)
        self._splitterMain = QSplitter(Qt.Vertical)

        #Add to Main Layout
        vbox.addWidget(self._splitterArea)

    def insert_central_container(self, container):
        self._mainContainer = container
        self._splitterMain.insertWidget(0, container)

    def insert_lateral_container(self, container):
        self._explorer = container
        self._splitterArea.insertWidget(0, container)

    def insert_bottom_container(self, container):
        self._misc = container
        self._splitterMain.insertWidget(1, container)

    def showEvent(self, event):
        #Rearrange widgets on Window
        self._splitterArea.insertWidget(0, self._splitterMain)
        #Show Event
        QWidget.showEvent(self, event)
        #Splitter Sizes
        height = [(self.height() / 3) * 2, self.height() / 3]
        self._splitterMain.setSizes(height)
        width = [(self.width() / 6) * 5, self.width() / 6]
        self._splitterArea.setSizes(width)
        self.change_misc_visibility()
        if bin(settings.UI_LAYOUT)[-1] == '1':
            self.splitter_central_rotate()
        if bin(settings.UI_LAYOUT >> 1)[-1] == '1':
            self.splitter_misc_rotate()
        if bin(settings.UI_LAYOUT >> 2)[-1] == '1':
            self.splitter_central_orientation()

    def change_misc_visibility(self):
        if self._misc.isVisible():
            self._misc.hide()
            widget = self._mainContainer.get_actual_widget()
            if widget:
                widget.setFocus()
        else:
            self._misc.show()
            self._misc.gain_focus()

    def change_main_visibility(self):
        if self._mainContainer.isVisible():
            self._mainContainer.hide()
        else:
            self._mainContainer.show()

    def change_explorer_visibility(self):
        if self._explorer.isVisible():
            self._explorer.hide()
        else:
            self._explorer.show()

    def splitter_central_rotate(self):
        w1, w2 = self._splitterArea.widget(0), self._splitterArea.widget(1)
        self._splitterArea.insertWidget(0, w2)
        self._splitterArea.insertWidget(1, w1)
        self.emit(SIGNAL("splitterCentralRotated()"))

    def splitter_central_orientation(self):
        if self._splitterArea.orientation() == Qt.Horizontal:
            self._splitterArea.setOrientation(Qt.Vertical)
        else:
            self._splitterArea.setOrientation(Qt.Horizontal)

    def splitter_misc_rotate(self):
        w1, w2 = self._splitterMain.widget(0), self._splitterMain.widget(1)
        self._splitterMain.insertWidget(0, w2)
        self._splitterMain.insertWidget(1, w1)

    def splitter_misc_orientation(self):
        if self._splitterMain.orientation() == Qt.Horizontal:
            self._splitterMain.setOrientation(Qt.Vertical)
        else:
            self._splitterMain.setOrientation(Qt.Horizontal)
